﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Raise3000.Code.Foundation;

namespace Raise3000.Code.Attribute_Calculator
{
    public class Attr_VPIPCalculator : ICalculator
    {
        private Dictionary<string, long> handsPlayed_ = new Dictionary<string, long>();
        private Dictionary<string, long> vpip_ = new Dictionary<string, long>();

        public Attr_VPIPCalculator()
        {
        }

        public override void DispatchAction(PlayerAction action, HandContextHelper handContextHelper)
        {
            // including BB?            
            if (handContextHelper.IsPreflop() && action.Move != ActionEnum.eFOLD)
            {
                vpip_[action.PlayerName]++;
            }
        }

        public override void RegisterPlayer(string playerName)
        {
            // ignore double registration
            if (!handsPlayed_.ContainsKey(playerName))
            {
                handsPlayed_.Add(playerName, 0);
                vpip_.Add(playerName, 0);
            }
        }

        public override void OnStartHand(HandContextHelper handContextHelper)
        {
            // Starting new hand - increment hands played counter.
            // Should only increment this value whose players contest in current hand. !!!
            List<string> currentPlayers = handContextHelper.GetCurrentPlayers();
            foreach (string player in currentPlayers)
            {
                handsPlayed_[player]++;
            }
        }

        public override void OnEndHand(HandContextHelper handConetxtHelper)
        {
            // Here nothing to do.            
        }

        public override StatisticalAttribute CreateStatisticalAttribute(string playerName)
        {
            double value = vpip_[playerName] / handsPlayed_[playerName];
            return new StatisticalAttribute("VP$IP", value, handsPlayed_[playerName]);
        }
    }
}
